<!--
//
// Trigger.vue
// vue3-element-admin
//
// Created by 区区电脑 on 2023/04/11.
//
-->
<template>
    <div class="Index">

        <!-- 内容 -->
        <div class="container">
            <el-tabs v-model="state.configType" @tabChange="searchClicked">
                <el-tab-pane :name="1" label="appsettings"/>
                <el-tab-pane :name="2" label="systemconfig"/>
            </el-tabs>
            <!-- 筛选区 -->
            <table-search-bar :config="requestBodyConfig" :model="requestBody" @search="searchClicked" style="border: none;box-shadow: none;padding: 0!important;">
                <template #button>
                    <el-button v-permission="permission.add" type="primary" @click="addData">
                        <b-icon name="bi-plus" size="20" />
                        添加
                    </el-button>
                </template>
            </table-search-bar>
            <!-- 功能按钮 -->
            <div class="table-buttons">

            </div>
            <!--checkbox设置 :checkbox-config="{trigger: 'row', highlight: true}" -->
            <!--radio设置 :radio-config="{trigger: 'row', highlight: true}" -->
            <vxe-table ref="VXETable" :column-config="{ resizable: false}" :data="state.list" :height="tableHeight" :loading="state.isLoading" :radio-config="{trigger: 'row', highlight: true}" :row-config="{isCurrent: true, isHover: true, keyField: 'id'}" :tree-config="{childrenField: 'children', reserve: true}" border round show-overflow size="small" @current-change="() => {}" @radio-change="() => {}">
                <template #empty>
                    <el-empty :image-size="100" />
                </template>
                <vxe-column fixed="left" title="#" type="seq" width="50" />
                <vxe-column field="configType" :fixed="$fixedColumn('left')" title="配置文件类型" width="140">
                    <template #default="{row}">
                        <el-tag v-if="row.configType === 1" effect="dark">appsettings</el-tag>
                        <el-tag v-if="row.configType === 2" effect="dark" type="success">systemconfig</el-tag>
                    </template>
                </vxe-column>
                <vxe-column field="key" :fixed="$fixedColumn('left')" width="200" title="Key"></vxe-column>
                <vxe-column field="valueType" title="数据类型" width="100">
                    <template #default="{row}">
                        <span v-if="row.valueType === 1">字符串</span>
                        <span v-if="row.valueType === 2">bool</span>
                        <span v-if="row.valueType === 3">int</span>
                        <span v-if="row.valueType === 4">float</span>
                        <span v-if="row.valueType === 5">时间</span>
                    </template>
                </vxe-column>
                <vxe-column field="value" title="默认值" width="180"></vxe-column>
                <vxe-column field="description" min-width="140" title="描述"></vxe-column>
                <vxe-column field="startVersion" align="center" title="开始版本" width="100">
                    <template #default="{row}">
                        <el-tag>V{{row.startVersion}}</el-tag>
                    </template>
                </vxe-column>
                <vxe-column field="endVersion" align="center" title="结束版本" width="100">
                    <template #default="{row}">
                        <el-tag v-if="row.endVersion" type="danger">V{{row.endVersion}}</el-tag>
                        <el-tag v-else type="success">长期</el-tag>
                    </template>
                </vxe-column>
                <vxe-column fixed="right" :resizable="false" field="action" title="操作" :width="$widthColumn(120)">
                    <template #default="{row}">
                        <table-action :actions="[
                            {title: '修改', permission: permission.edit, type: 'primary', ok: editData},
                            {title: '删除', permission: permission.delete, type: 'danger', ok: deleteData, confirm: '您确定要删除当前数据么?'},
                        ]" :data="row" />
                    </template>
                </vxe-column>
            </vxe-table>
            <div class="page-config">
                <vxe-pager v-model:current-page="requestParams.pageIndex" v-model:page-size="requestParams.pageSize" :layouts="['PrevJump', 'PrevPage', 'JumpNumber', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']" :page-sizes="pageConfig.sizes" :total="pageConfig.total" background size="mini" @page-change="onPageChange" />
            </div>
        </div>
    </div>
    <EditForm ref="editForm" />
</template>

<script setup>
import {ref, reactive, computed, onMounted} from "vue";
import {useStore} from "vuex";
import {useRouter} from "vue-router";
import * as api from "./api.js"
import EditForm from "./EditForm.vue"

const {$message, $fixedColumn, $widthColumn} = window;

const props = defineProps({});
const emits = defineEmits();

// 路由
const router = useRouter();
// store
const store = useStore();
const tableHeight = computed(() => store.getters.layoutType === "mobile" ? "" : store.getters.containerHeight - 195)
// 响应式数据
const state = reactive({
    // 表格数据加载中
    isLoading: false,
    // 表格数据
    list: [],
    // 配置类型
    configType: 1
})

// 当前页面功能权限
const permission = {
    add: "ProgramConfig:AddProgramConfig",
    delete: "ProgramConfig:DeleteProgramConfig",
    edit: "ProgramConfig:UpdateProgramConfig",
};

// 请求query参数
const requestParams = reactive({
    orderCol: "id",
    isDesc: true,
    pageIndex: 1,
    pageSize: 20,
});
// 请求body参数
const requestBody = reactive({
    name: null,
})
// 筛选区域配置
const requestBodyConfig = reactive({
    programId: {label: "程序", component: "el-select", data: []},
    // configType: {label: "配置文件类型", component: "el-select", data: [{name: "appsettings", value: 1}, {name: "systemconfig", value: 2}]},
    key: {label: "配置项 Key", component: "el-input"},
    startVersion: {label: "开始版本", component: "el-input"},
    endVersion: {label: "结束版本", component: "el-input"},
    // code: {label: "编号", component: "el-input", inMore: true},
    //sort: {label: "排序", component: "el-select", data: [{name: "1", value: 1}, {name: "2", value: 2}], inMore: true},
    //createdTime: {label: "创建时间", component: "el-date-picker", format: "YYYY-MM-DD", inMore: true},
    //updateTime: {label: "更新时间", component: "el-date-picker-datetime", format: "YYYY-MM-DD HH:mm:ss", inMore: true}
})
// 分页配置
const pageConfig = reactive({
    total: 0,
    sizes: [10, 20, 50, 100, 200]
})


const editForm = ref();

// 添加
function addData() {
    editForm.value.show({
        title: "添加",
        ok: () => {
            state.configType = 1
            reload(true);
        }
    })
}

// 删除
async function deleteData({id}) {
    console.danger("删除: ", id)
    const {success, data} = await api.DeleteProgramConfig({id});
    if (!success) return 0;
    reload();
    $message.success("删除成功")
}

// 修改
function editData(row) {
    editForm.value.show({
        title: "修改",
        options: {...row},
        ok: () => {
            reload();
        }
    })
}

// 点击搜索
function searchClicked() {
    console.primary("搜索条件: ", requestBody)
    reload(true);
}

// 查询列表
async function loadList() {
    const params = {
        ...requestParams
    };
    const body = {
        ...requestBody,
        configType: state.configType
    };
    state.isLoading = true;
    const {success, data} = await api.GetProgramConfigPageList(params, body);
    state.isLoading = false;
    if (!success) return 0;
    const {items, totalCount} = data;
    state.list = items;
    pageConfig.total = totalCount;
}

// 分页事件
function onPageChange({type}) {
    if (type === "current") {
        loadList()
    }
    if (type === "size") {
        reload(true)
    }
}

// 刷新
function reload(resetPageIndex) {
    if (resetPageIndex) requestParams.pageIndex = 1;
    loadList()
}


onMounted(() => {
    api.GetProgramList({}).then(res => {
        requestBodyConfig.programId.data = (res.data || []).map(item => {
            item.value = item.id
            return item;
        });
    })
    loadList()
})
</script>

<style lang="scss" scoped>
.Index {
    margin: 10px;
    width: calc(100% - 20px);
    box-sizing: border-box;

    .container {
        padding: 0 10px 10px 10px;
        background: #fff;
        margin-top: 10px;
        box-shadow: 0 0 10px -2px rgba(0, 0, 0, 0.1);
    }
}
</style>
